package com.tencent.qcloud.tim.uikit.component.dialog

import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import androidx.recyclerview.widget.LinearLayoutManager
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.impl.FullScreenPopupView
import com.tencent.qcloud.tim.uikit.R
import com.tencent.qcloud.tim.uikit.modules.group.member.GroupMemberInfo
import com.tencent.qcloud.tim.uikit.modules.group.member.GroupMemberManagerAdapter
import kotlinx.android.synthetic.main.dialog_group_member.view.*

/**
 *


 * @date  2020/7/23  14:03
 * @version 1.0
 */
class GroupMemberDialog(context: Context,private val data: List<GroupMemberInfo>) : FullScreenPopupView(context) {

    private var mAdapter: GroupMemberManagerAdapter? = null

    companion object {
        fun load(context: Context, data: List<GroupMemberInfo>) {
            XPopup.Builder(context)
                    .hasStatusBar(true)
                    .asCustom(GroupMemberDialog(context, data))
                    .show()
        }
    }

    override fun getImplLayoutId(): Int = R.layout.dialog_group_member


    override fun onCreate() {
        super.onCreate()
        etSearch.addTextChangedListener(editListener)
        mAdapter = GroupMemberManagerAdapter(R.layout.group_member_adpater_new, mutableListOf())
        mRecycler.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        mRecycler.adapter = mAdapter
        tvDismiss.setOnClickListener { dismiss() }
    }


    private fun doSearch(key: String) {
        val list = data.filter { it.nameCard.contains(key) || it.nickName.contains(key) }.toMutableList()
        mAdapter?.setNewInstance(list)
        mAdapter?.notifyDataSetChanged()
    }


    private val editListener = object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {
            s?.run {
                doSearch(this.toString())
            }
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

        }

    }

}